package com.su.kddq.utils;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 个推工具
 * @author sx
 */
@Component
@Slf4j
public class GeTuiUtils {

    @Autowired
    private ApiHelper apiHelper;

    /**
     * 消息推送（离线推送）单cid推送
     *
     * @param cid
     * @param title
     * @param content
     */
    public void pushToSingleByCid(String cid, String title, String content) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置接收人信息
        Audience audience = new Audience();
        PushMessage pushMessage = new PushMessage();
        Settings settings = new Settings();
        Strategy strategy = new Strategy();
        strategy.setDef(1);
        GTNotification gtNotification = new GTNotification();
        gtNotification.setTitle(title);
        gtNotification.setBody(content);
        gtNotification.setClickType("startapp");
        gtNotification.setChannelLevel("3");
        gtNotification.setBadgeAddNum("1");
        gtNotification.setBigText(content);
        settings.setTtl(24*60*60*1000);
        settings.setStrategy(strategy);
        pushMessage.setNotification(gtNotification);
        // 进行cid单推
        audience.addCid(cid);
        pushDTO.setRequestId(System.currentTimeMillis()+"");
        pushDTO.setAudience(audience);
        pushDTO.setPushMessage(pushMessage);
        pushDTO.setSettings(settings);
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            log.info("消息推送成功==>code:" + apiResult.getMsg());
        } else {
            log.error("消息推送失败==>code:" + apiResult.getMsg());
        }
    }
}
